package mysql

import (
	"gitignore/gitignorerpc/gitignorerpc"
	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	VideoId        int    `gorm:"type:int;comment:视频ID"`
	UserId         int    `gorm:"type:int;comment:用户ID"`
	CommentContent string `gorm:"type:int;comment:评论内容"`
}

func (c *Comment) FindCommentByAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) FindCommentByList(db *gorm.DB, list []*gitignorerpc.CommentList, in *gitignorerpc.CommentListRequest) []*gitignorerpc.CommentList {
	db.
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)

	return list
}

func (c *Comment) FindCommentByDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(c).Error
}

func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
